package com.its.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.its.manager.mapper.AdminMapper;
import com.its.manager.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;
@Service
public class AdminDetailServiceImpl implements UserDetailsService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名查询管理员信息
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Admin admin = adminMapper.selectOne(queryWrapper);

        if (admin == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        // 不查角色、不查权限，只返回用户名和密码
        return new User(admin.getUsername(), admin.getPassword(), Collections.emptyList());
        // 如果你想赋一个默认权限，可写为：
        // return new User(admin.getUsername(), admin.getPassword(),
        //     Collections.singletonList(new SimpleGrantedAuthority("ROLE_ADMIN")));
    }
}
